﻿using System;
using System.Collections;
using System.Text;
using System.Xml;

namespace MPMovieCatalog.Domain
{
    [Serializable()]
    public class MovieInfo : MovieShortInfo
    {
        private ArrayList m_genres = new ArrayList();

        private ArrayList m_actors = new ArrayList();

        private ArrayList m_files = new ArrayList();

        public void AddActor(Actor actor)
        {
            if (m_actors == null)
            {
                m_actors = new ArrayList();
            }
            m_actors.Add(actor);
        }

        public void AddGenre(Genre genre)
        {
            if (m_genres == null)
            {
                m_genres = new ArrayList();
            }
            m_genres.Add(genre);
        }

        public void AddFile(ShareFile file)
        {
            if (m_files == null)
            {
                m_files = new ArrayList();
            }
            m_files.Add(file);
        }

        public XmlDocument ToXML()
        {
            XmlDocument movieXML = new XmlDocument();
            XmlElement root = movieXML.CreateElement("movieInfo");
            movieXML.AppendChild(root);

            ToXML(root);
            return movieXML;
        }

        public void ToXML(XmlElement root)
        {
            XmlDocument document = root.OwnerDocument;

            XmlElement element = document.CreateElement("title");
            element.AppendChild(document.CreateTextNode(this.Name));
            root.AppendChild(element);

            element = document.CreateElement("tagline");
            element.AppendChild(document.CreateTextNode(this.Tagline));
            root.AppendChild(element);

            element = document.CreateElement("description");
            element.AppendChild(document.CreateTextNode(this.Description));
            root.AppendChild(element);

            element = document.CreateElement("imdbRating");
            element.AppendChild(document.CreateTextNode(this.IMDBRating.Replace(',', '.')));
            root.AppendChild(element);

            element = document.CreateElement("imdbNumber");
            element.AppendChild(document.CreateTextNode(this.IMDBNumber));
            root.AppendChild(element);

            element = document.CreateElement("imdbVotes");
            element.AppendChild(document.CreateTextNode(this.IMDBVotes));
            root.AppendChild(element);

            element = document.CreateElement("year");
            element.AppendChild(document.CreateTextNode(this.Year));
            root.AppendChild(element);

            element = document.CreateElement("runtime");
            element.AppendChild(document.CreateTextNode(this.Runtime));
            root.AppendChild(element);

            element = document.CreateElement("mpaaRating");
            element.AppendChild(document.CreateTextNode(this.MPAARating));
            root.AppendChild(element);

            element = document.CreateElement("director");
            element.AppendChild(document.CreateTextNode(this.Director));
            root.AppendChild(element);

            element = document.CreateElement("writer");
            element.AppendChild(document.CreateTextNode(this.Writer));
            root.AppendChild(element);

            element = document.CreateElement("picture");
            element.AppendChild(document.CreateTextNode(this.PictureFileName));
            root.AppendChild(element);

            element = document.CreateElement("isWatched");
            element.AppendChild(document.CreateTextNode(this.IsWatched.ToString().ToLower()));
            root.AppendChild(element);

            element = document.CreateElement("actors");
            root.AppendChild(element);
            foreach (Actor actor in m_actors)
            {
                XmlElement child = document.CreateElement("actor");
                child.AppendChild(document.CreateTextNode(actor.Name));
                element.AppendChild(child);
            }

            element = document.CreateElement("genres");
            root.AppendChild(element);
            foreach (Genre genre in m_genres)
            {
                XmlElement child = document.CreateElement("genre");
                child.AppendChild(document.CreateTextNode(genre.Name));
                element.AppendChild(child);
            }

            element = document.CreateElement("files");
            root.AppendChild(element);
            foreach (ShareFile file in m_files)
            {
                XmlElement child = document.CreateElement("file");
                child.AppendChild(document.CreateTextNode(file.Name));
                element.AppendChild(child);
            }
        }

        public string Picture { get; set; }

        public string PictureFileName { get; set; }

        public string Thumb { get; set; }

        public string Tagline { get; set; }

        public string Description { get; set; }

        public string IMDBRating { get; set; }

        public string IMDBNumber { get; set; }

        public string IMDBVotes { get; set; }

        public string Year { get; set; }

        public string Runtime { get; set; }

        public string MPAARating { get; set; }

        public string Director { get; set; }

        public string Writer { get; set; }

        public bool IsWatched { get; set; }

        public ArrayList Genres
        {
            get { return m_genres; }
            set { m_genres = value; }
        }

        public ArrayList Actors
        {
            get { return m_actors; }
            set { m_actors = value; }
        }

        public ArrayList Files
        {
            get { return m_files; }
            set { m_files = value; }
        }

        public string ActorsString
        {
            get
            {
                StringBuilder actors = new StringBuilder();
                foreach (Actor actor in Actors)
                {
                    if (actors.Length > 0)
                    {
                        actors.Append("\n");
                    }
                    actors.Append(actor.Name);
                }
                return actors.ToString();
            }
        }

        public string GenresString
        {
            get
            {
                StringBuilder genres = new StringBuilder();
                foreach (Genre genre in Genres)
                {
                    if (genres.Length > 0)
                    {
                        genres.Append(" / ");
                    }
                    genres.Append(genre.Name);
                }
                return genres.ToString();
            }
        }
    }
}
